package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.model.dto.UserDTO;
import com.example.mybatisplus.service.UserService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AdminService;
import com.example.mybatisplus.model.domain.Admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author zhang
 * @since 2021-07-05
 * @version v1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    private final Logger logger = LoggerFactory.getLogger( AdminController.class );

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Admin  admin =  adminService.getById(id);
        return JsonResponse.success(admin);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        adminService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateAdmin(@PathVariable("id") Integer  id,Admin  admin) throws Exception {
        admin.setId(id);
        adminService.updateById(admin);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Admin
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Admin  admin) throws Exception {
        adminService.save(admin);
        return JsonResponse.success(null);
    }


    /**
     *
     * @param
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/register")
    public String register(Model model,String adminName,String password){
        Admin admin = new Admin();
        admin.setAdminName(adminName);
        admin.setPassword(password);
        admin.setRegisterTime(new Date().getTime());
        admin.setMember(1);
        String register = adminService.register(admin);
        return register;
    }

    @PostMapping("/login")
    @ResponseBody
    public String login(Model model,String adminname,String password) {
        Admin admin1 = adminService.login(adminname, password);
        if (admin1 == null) {
            return "1002";
        } else
            return "1000";
    }

    /**
     * 用户列表获取接口
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/admin_user_list_data")
    @ResponseBody
    public JsonResponse adminUserList(
            @RequestParam(required = false,defaultValue = "1") Integer pageNo,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize
    ){
        Page<User> userlist = userService.page(new Page<>(pageNo,pageSize));
        return JsonResponse.success(userlist);
    }

    /**
     * 添加用户接口
     * @param userName
     * @param password
     * @param phone
     * @param address
     * @param remainder
     * @param member
     * @return
     */
    @PostMapping("/add_user")
    @ResponseBody
    public JsonResponse addUser(
            @RequestParam(required = true) String userName,
            @RequestParam(required = false,defaultValue = "123") String password,
            @RequestParam(required = true) String phone,
            @RequestParam(required = true) String address,
            @RequestParam(required = false) Long remainder,
            @RequestParam(required = false) Integer member
    ){
        User user = new User()
                .setUserName(userName)
                .setPassword(password)
                .setPhone(phone)
                .setAddress(address)
                .setRegisterTime(new Date().getTime())
                .setRemainder(remainder)
                .setMember(member);
        Integer i = adminService.adminAddUser(user);
        return JsonResponse.success(i);
    }

    /**
     * 编辑单个用户
     * @param userName
     * @param password
     * @param phone
     * @param address
     * @param remainder
     * @param member
     * @return
     */
    @PostMapping("/edit_user")
    @ResponseBody
    public JsonResponse editUser(
            @RequestParam(required = true) Integer id,
            @RequestParam(required = true) String userName,
            @RequestParam(required = false,defaultValue = "123") String password,
            @RequestParam(required = true) String phone,
            @RequestParam(required = true) String address,
            @RequestParam(required = false) Long remainder,
            @RequestParam(required = false) Integer member
    ){
        User user = new User()
                .setId(id)
                .setUserName(userName)
                .setPassword(password)
                .setPhone(phone)
                .setAddress(address)
                .setRegisterTime(new Date().getTime())
                .setRemainder(remainder)
                .setMember(member);
        Integer i = adminService.adminEditUser(user);
        return JsonResponse.success(i);
    }

    /**
     * 根据name查询
     * */
    /**
     * 描述：根据Id 查询
     *
     */
    @PostMapping("/select")
    @ResponseBody
    public JsonResponse adminUserSelect(Model model,@RequestParam(required = false) String userName){
        System.out.println(userName);
        List<User> user1 = adminService.select(userName);
        return JsonResponse.success(user1);
    }
    /**
     * 描述：根据Id多行删除user
     */
    @PostMapping("/deletes")
    @ResponseBody
    public JsonResponse deletes(UserDTO userDTO) {
        userService.removeByIds(userDTO.getIds());
        return JsonResponse.success(null);
    }
}

